/**
 * Created by MC on 2017/4/26.
 */

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}


/*
* 显示上传图片的缩略图
* */
function show_upload_file(fileId,imgId){
    var resultFile = document.getElementById(fileId).files[0];
    if (resultFile) {
        var reader = new FileReader();
        reader.readAsDataURL(resultFile);
        reader.onload = function (e) {
            var urlData = this.result;
            document.getElementById(imgId).setAttribute('src',urlData);
        };

    }
}


/*
* 显示添加部门的模态框
* */
function show_add_department_model(companyId){
    var companyName = $('#company-' + companyId).data('name');
    $('#dep-com-id').val(companyId);
    $('#company-name').text(companyName);
    $('#add-department-model').modal('show');
}

//基于bootstrap的loading
var loading = {
    init:function(){
        $("body").append("<!-- loading -->" +
            "<div id='loading-box'>" +
            "<div class='modal fade' id='loading' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' data-backdrop='static'>" +
            "<div class='modal-dialog' role='document'>" +
            "<div class='modal-content'>" +
            "<div class='modal-header'>" +
            "<h4 class='modal-title' id='myModalLabel'>提示</h4>" +
            "</div>" +
            "<div id='loadingText' class='modal-body'>" +
            "<span class='glyphicon glyphicon-refresh' aria-hidden='true'>1</span>" +
            "处理中，请稍候。。。" +
            "</div>" +
            "</div>" +
            "</div>" +
            "</div>" +
            "</div>"
        );
    },
    show:function(){
        if(!$('loading-box')){
            this.init();
        }
        $("#loadingText").html(text);
        $("#loading").modal("show");
    },
    close:function(){
        $("#loading").modal("hide");
    }
};


/*
* 审核分会订单
* */
function check_chapter_order(){
    var checkResult = $("input[name='company-check-result']:checked").val();
    var checkMsg    = $("#check-msg").val();
    var orderId     = getQueryString('orderId');
    var url = _system.host + "admin_system/chapter/handleOrder";

    var data= {
        checkResult : checkResult,
        checkMsg    : checkMsg,
        orderId     : orderId,

    };


    $.post(url,data,function(res){
        alert(res.message);
        if(res.code == 200){
            location.reload();
        }
    });
}


/*
* 获得城市信息
* */
function get_cities(){
    var url         = _system.host + "service/region/get_cities";
    var provinceId  = $('#province').val();
    var data = {provinceId:provinceId};

    $.post(url,data,function(res){
        var cities  = res.data.cities;
        var options = "";
        for(var city of cities){
            options += "<option value='"+city.region_id+"'>"+city.region_name+"</option>";
        }
        $('#city').html(options);
    });

}



